package com.supermarket.model;

import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * 销售明细实体类
 * 对应数据库中的sales_details表
 */
public class SalesDetail {
    private int id;
    private int orderId;
    private int productId;
    private String productCode;
    private String productName;
    private BigDecimal quantity;
    private BigDecimal unitPrice;
    private Integer promotionId;
    private String promotionName;
    private BigDecimal discountAmount;
    private BigDecimal subtotal;
    private Timestamp createdAt;

    // 构造函数
    public SalesDetail() {
        this.discountAmount = BigDecimal.ZERO;
    }

    public SalesDetail(int productId, String productCode, String productName,
            BigDecimal quantity, BigDecimal unitPrice) {
        this();
        this.productId = productId;
        this.productCode = productCode;
        this.productName = productName;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
        this.subtotal = quantity.multiply(unitPrice);
    }

    // Getter和Setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
        // 自动计算小计
        if (this.unitPrice != null) {
            this.subtotal = quantity.multiply(unitPrice)
                    .subtract(discountAmount != null ? discountAmount : BigDecimal.ZERO);
        }
    }

    public BigDecimal getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(BigDecimal unitPrice) {
        this.unitPrice = unitPrice;
        // 自动计算小计
        if (this.quantity != null) {
            this.subtotal = quantity.multiply(unitPrice)
                    .subtract(discountAmount != null ? discountAmount : BigDecimal.ZERO);
        }
    }

    public Integer getPromotionId() {
        return promotionId;
    }

    public void setPromotionId(Integer promotionId) {
        this.promotionId = promotionId;
    }

    public String getPromotionName() {
        return promotionName;
    }

    public void setPromotionName(String promotionName) {
        this.promotionName = promotionName;
    }

    public BigDecimal getDiscountAmount() {
        return discountAmount;
    }

    public void setDiscountAmount(BigDecimal discountAmount) {
        this.discountAmount = discountAmount;
        // 重新计算小计
        if (this.quantity != null && this.unitPrice != null) {
            this.subtotal = quantity.multiply(unitPrice)
                    .subtract(discountAmount != null ? discountAmount : BigDecimal.ZERO);
        }
    }

    public BigDecimal getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(BigDecimal subtotal) {
        this.subtotal = subtotal;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public String toString() {
        return "SalesDetail{" +
                "id=" + id +
                ", orderId=" + orderId +
                ", productId=" + productId +
                ", productCode='" + productCode + '\'' +
                ", productName='" + productName + '\'' +
                ", quantity=" + quantity +
                ", unitPrice=" + unitPrice +
                ", promotionId=" + promotionId +
                ", promotionName='" + promotionName + '\'' +
                ", discountAmount=" + discountAmount +
                ", subtotal=" + subtotal +
                ", createdAt=" + createdAt +
                '}';
    }
}
